<template>
  <div class="login-wrap">
    <div class="login-contain">
      <div class="login-header">
        <p></p>
      </div>
      <div class="form-group">
        <div class="form-item">
          <label for="username">
            <img src="../../assets/loginImg/emails.png" />
          </label>
          <input id="username" type="text" maxlength="22" placeholder="请输入邮箱地址"  v-model="email" @input="ema"/>
          <img :src="isok==true?yes:err" alt="" v-show="img">
        </div>
        <div class="form-item dis-get">
          <label for="password">
            <img src="../../assets/loginImg/duanxin.png" alt="" />
          </label>
          <input id="iphoneCode" type="text" maxlength="6" placeholder="请输入验证码" v-model="codeid"/>
          <span class="getNum-iphone" @click="getCode()">获取验证码 <b>{{count}}</b></span>
        </div>
      </div>
      <div class="button-group">
        <van-button type="default" class="login-btn" id="doLogin" @click="doLoginEma">登录</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant';
export default {
  name: "WorkspaceJsonLogin",

  data() {
    return {
      img:false,
      email:'',
      codeid:'',
      isok:false,
      yes:require('../../assets/loginImg/yes.png'),
      err:require('../../assets/loginImg/err.png'),
      // 验证码
      count:'',
      timer: null,
    };
  },

  mounted() {},

  methods: {
    // 邮箱验证
    ema(){
      this.img = true
      let userReg = new RegExp("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
      if (this.email == "") {
        this.isok = false
      } else if (userReg.test(this.email)) {
        this.isok = true
      } else {
        this.isok = false
      }
    },

    // 倒计时
    getCode(){
      if(this.isok == true){
        const TIME_COUNT = 60;
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= TIME_COUNT) {
            this.count--;
           } else {
             clearInterval(this.timer);
            this.timer = null;
            this.count = ''
           }
          }, 1000)

          // 倒计时开始发送邮箱
          this.$http.post('/emailS',{
            email:this.email
          }).then(res=>{
            if(res.data.code==200){
              Toast.success(res.data.message)
            }else if(res.data.code==202){
              // 未绑定该邮箱后清除定时器
              Toast.fail(res.data.message)
              clearInterval(this.timer);
              this.timer = null;
              this.count = ''
            }
            // console.log(res);
          }).catch(err=>{
            Toast.fail('发送失败，请稍后重试')
          })
          
         }
      }else{
        Toast.fail('请输入格式正确邮箱');
      }
   } ,
    // 收到验证码开始登录
    doLoginEma(){
      if(this.email==''|| this.codeid=='' || this.isok==false){
        Toast.fail('请完成表单验证')
      }else{
        this.$http.post('/emailLogin',{
          email:this.email,
          codeid:this.codeid
        }).then(res=>{
          // console.log(res);
          if(res.data.code == 200){
             // 用户名加密存储
            let encodedData = window.btoa(window.encodeURIComponent(res.data.data[0].uname));
            sessionStorage.setItem('cookie-session',encodedData)
            Toast.success('登录成功');
            this.$router.replace('/me')
            
          }else if(res.data.code == 203){
            Toast.fail('验证码错误')
          }
        }).catch(err=>{
          Toast.fail('登录失败，请稍后重试')
        })
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.login-wrap{
    background: url('../../assets/loginImg/bac-bg.jpg') no-repeat;
    background-size:cover ;

    /* background: linear-gradient(rgba(248, 248, 248,0.1), rgba(248, 248, 248,0.1)), url('../assets/loginImg/bac-bg.jpg') no-repeat; */
}

a, li {
    list-style: none;
}
a {
    text-decoration: none;
    color: black;
}

.login-contain {
    margin: 0!important;
    width: 100%;
    min-height: 800px;
}
.login-header {
    padding: 5%;
}
.login-header p {
    margin: 150px 0;
    font-size: 32px;
    color: #ffffff;
    font-weight: bold;
    text-align: center;
    letter-spacing: 2px;
    text-shadow: 0.1em 0.15em 0.1em #55a6ad
}
.login-logo {
    padding: 5%;
}
.login-logo img {
    width: 55px;
    height: 55px;
    border-radius: 50%;
}
.login-logo p {
    color: #ffffff;
    font-size: 14px;
    text-align: center;
    letter-spacing: 2px;
    margin-top: 2%;
}
.form-group {
    padding: 5%;
}
.form-group .form-item {
    margin-top: 5%;
    padding:0 10px;
    border-radius: 20px;
    background-color: rgba(59, 208, 245, 0.5);
     display: flex;
    justify-content: flex-start;
    >img{
      margin:10px 0 0 15px;
      width: 25px;
      height: 25px;
    }
}
.form-group .form-item input {
    outline: none;
    border: 0;
    background-color: transparent;
    color: #000000;
    height: 40px;
    font-size: 18px;
    width: 70%;
    margin-left: 12%;
}
.form-group .form-item input::-webkit-input-placeholder {
    /* WebKit browsers */
    color: #ffffff;
    font-size: 16px;
}
.form-group .form-item input:-moz-placeholder {
    /* Mozilla Firefox 4 to 18 */
    color: #ffffff;
    font-size: 16px;
}
.form-group .form-item input::-moz-placeholder {
    /* Mozilla Firefox 19+ */
    color: #ffffff;
    font-size: 16px;
}
.form-group .form-item input:-ms-input-placeholder {
    /* Internet Explorer 10+ */
    color: #ffffff;
    font-size: 16px;
}
.form-group .form-item label img {
    width: 25px;
    position: absolute;
    margin-top: 5px;
}
.form-group .form-item button {
    outline: none;
    background: transparent;
    border: 1px #00cc99 dashed;
    color: #ffffff;
    height: 30px;
    border-radius: 5px;
    float: right;
    padding: 1%;
}
.button-group {
    padding: 5%;
}
.button-group button {
    outline: none;
    border: 0;
    width: 90%;
    height: 40px;
    margin-top: 4%;
    border-radius: 20px;
    margin-left: 4%;
    color: #ffffff;
    font-size: 18px;
}
.button-group .login-btn {
    background-color: #ffffff;
    color: #74C3CA;
}

.order-login {
    padding: 5%;
}
.order-login-line {
    display: block;
    position: relative;
    text-align: center;
    font-size: 14px;
    color: #ffffff;
}
.order-login-line:before,
.order-login-line:after {
    content: '';
    position: absolute;
    top: 50%;
    background: #ffffff;
    width: 20%;
    height: 1px;
}
.order-login-line:before {
    left: 10%;
}
.order-login-line:after {
    right: 10%;
}
.order-login-box {
    display: flex;
    width: 100%;
    justify-content:center;
    margin-top: 20px;
}
.order-login-box div{
    flex: 1;
    text-align: center;
}
.order-login-box div p{
    text-align: center;
    font-size: 14px;
    color: #ffffff;
    
}
.display{
    a{
        margin: 0 30px;
    }
    display: flex;
    justify-content: center;

}
.dis-get{
  display: flex;
}
#iphoneCode{
  width: 50%;
}
.getNum-iphone{
  line-height: 42px;
  text-align: center;
  margin-left:20px ;
}


</style>